package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult<Course> list(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Course> list = courseMapper.selectList( name, subject, target);
        Page<Course> p =  (Page<Course>) list ;
        return new PageResult<>(p.getTotal(),p.getResult());
    }
    @Override
    public void delete(Integer id) {
        courseMapper.delete(id);
        System.out.println("删除: " + id);
    }
    @Override
    public void add(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        course.setCreateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }

    @Override
    public Course selectById(Integer id) {
        return courseMapper.selectById(id);

    }

    @Override
    public void update( Course course) {
        course.setUpdateTime(LocalDateTime.now());
        course.setCreateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    @Override
    public List<Course> listAll(Course course) {
        return courseMapper.selectList(course);
    }

    @Override
    public List<Course> listBySubject(Integer subject) {
        return courseMapper.listBySubject(subject);
    }


}
